/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.common.query.expression;

import java.util.Collection;

/**
 * <pre>
 * 
 * 简单属性表达式
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class SimplePropertyExpression implements ParameterCriterion {

	private String operator;
	private String propertyName;
	private Object otherPropertyName;

	private SimplePropertyExpression(String operator, String propertyName, String otherPropertyName) {
		this.operator = operator;
		this.propertyName = propertyName;
		this.otherPropertyName = otherPropertyName;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getJpql(java.lang.
	 *      StringBuilder)
	 */
	@Override
	public StringBuilder getJpql(StringBuilder jpql, String alias) {
		jpql.append(AND);
		jpql.append(propertyName);
		jpql.append(operator);
		jpql.append(otherPropertyName);
		return jpql;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getParameters(java
	 *      .util. Collection)
	 */
	@Override
	public Collection<Object> getParameters(Collection<Object> parameters) {
		return parameters;
	}

	/**
	 * 构建表达式
	 * 
	 * @param operator
	 *            运算符
	 * @param propertyName
	 *            属性名
	 * @param otherPropertyName
	 *            其他属性
	 * @return 返回简单属性表达式
	 */
	public static SimplePropertyExpression getExpression(String operator, String propertyName, String otherPropertyName) {
		return new SimplePropertyExpression(operator, propertyName, otherPropertyName);
	}

	/**
	 * 获取不等于的表达试
	 * 
	 * @param propertyName
	 *            属性名
	 * @param otherPropertyName
	 *            其他属性
	 * @return 返回不等于的属性表达式
	 */
	public static SimplePropertyExpression getNeExpression(String propertyName, String otherPropertyName) {
		return getExpression(NE, propertyName, otherPropertyName);
	}

	/**
	 * 获取等于的表达试
	 * 
	 * @param propertyName
	 *            属性名
	 * @param otherPropertyName
	 *            其他属性
	 * @return 返回等于的属性表达式
	 */
	public static SimplePropertyExpression getEqExpression(String propertyName, String otherPropertyName) {
		return getExpression(EQ, propertyName, otherPropertyName);
	}

	/**
	 * 获取大于等于的表达试
	 * 
	 * @param propertyName
	 *            属性名
	 * @param otherPropertyName
	 *            其他属性
	 * @return 返回大于等于的属性表达式
	 */
	public static SimplePropertyExpression getGeExpression(String propertyName, String otherPropertyName) {
		return getExpression(GE, propertyName, otherPropertyName);
	}

	/**
	 * 获取大于的表达试
	 * 
	 * @param propertyName
	 *            属性名
	 * @param otherPropertyName
	 *            其他属性
	 * @return 返回大于的属性表达式
	 */
	public static SimplePropertyExpression getGtExpression(String propertyName, String otherPropertyName) {
		return getExpression(GT, propertyName, otherPropertyName);
	}

	/**
	 * 获取小于等于的表达试
	 * 
	 * @param propertyName
	 *            属性名
	 * @param otherPropertyName
	 *            其他属性
	 * @return 返回小于等于的属性表达式
	 */
	public static SimplePropertyExpression getLeExpression(String propertyName, String otherPropertyName) {
		return getExpression(LE, propertyName, otherPropertyName);
	}

	/**
	 * 获取小于的表达试
	 * 
	 * @param propertyName
	 *            属性名
	 * @param otherPropertyName
	 *            其他属性
	 * @return 返回小于的属性表达式
	 */
	public static SimplePropertyExpression getLtExpression(String propertyName, String otherPropertyName) {
		return getExpression(LT, propertyName, otherPropertyName);
	}
}
